{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "cedf868076a2"
   },
   "source": [
    "##### Copyright 2020 The Cirq Developers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "cellView": "form",
    "id": "906e07f6e562"
   },
   "outputs": [],
   "source": [
    "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "# https://www.apache.org/licenses/LICENSE-2.0\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "166af89a7bc3"
   },
   "source": [
    "# Neutral atom device class"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "416c50754585"
   },
   "source": [
    "<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://www.example.org/cirq/tutorials/educators/neutral_atom\"><img src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" />View on QuantumLib</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://colab.research.google.com/github/quantumlib/Cirq/blob/master/docs/tutorials/educators/neutral_atom.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://github.com/quantumlib/Cirq/blob/master/docs/tutorials/educators/neutral_atom.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a href=\"https://storage.googleapis.com/tensorflow_docs/Cirq/docs/tutorials/educators/neutral_atom.ipynb\"><img src=\"https://www.tensorflow.org/images/download_logo_32px.png\" />Download notebook</a>\n",
    "  </td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "bNlUyYQcgMF-"
   },
   "source": [
    "This tutorial provides an introduction to making circuits that are compatible with neutral atom devices.\n",
    "\n",
    "Neutral atom devices implement quantum gates in one of two ways. One method is by hitting the entire qubit array with microwaves to simultaneously act on every qubit. This method implements global $XY$ gates which take up to $100$ microseconds to perform. Alternatively, we can shine laser light on some fraction of the array. Gates of this type typically take around $1$ microsecond to perform. This method can act on one or more qubits at a time up to some limit dictated by the available laser power and the beam steering system used to address the qubits. Each category in the native gate set has its own limit, discussed more below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "id": "GTjMbjyAfJCK"
   },
   "outputs": [],
   "source": [
    "try:\n",
    "    import cirq\n",
    "except ImportError:\n",
    "    print(\"installing cirq...\")\n",
    "    !pip install cirq --quiet\n",
    "    print(\"installed cirq.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "id": "4927d86fa122"
   },
   "outputs": [],
   "source": [
    "from math import pi\n",
    "\n",
    "import cirq"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "b23c49ab588f"
   },
   "source": [
    "## Defining a `NeutralAtomDevice`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Z0X2AWrhrcHR"
   },
   "source": [
    "To define a `NeutralAtomDevice`, we specify\n",
    "\n",
    "- The set of qubits in the device.\n",
    "- The maximum duration of gates and measurements.\n",
    "- `max_parallel_z`: The maximum number of single qubit $Z$ rotations that can be applied in parallel.\n",
    "- `max_parallel_xy`: The maximum number of single qubit $XY$ rotations that can be applied in parallel.\n",
    "- `max_parallel_c`: The maximum number of atoms that can be affected by controlled gates simultaneously.\n",
    "  - Note that `max_parallel_c` must be less than or equal to the minimum of `max_parallel_z` and `max_parallel_xy`.\n",
    "- `control_radius`: The maximum allowed distance between atoms acted on by controlled gates.\n",
    "\n",
    "We show an example of defining a `NeutralAtomDevice` below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "id": "On6Wrh3XhSPO"
   },
   "outputs": [],
   "source": [
    "\"\"\"Defining a NeutralAtomDevice.\"\"\"\n",
    "# Define milliseconds and microseconds for convenience.\n",
    "ms = cirq.Duration(nanos=10**6)\n",
    "us = cirq.Duration(nanos=10**3)\n",
    "\n",
    "# Create a NeutralAtomDevice\n",
    "neutral_atom_device = cirq.NeutralAtomDevice(\n",
    "    qubits=cirq.GridQubit.rect(2, 3),\n",
    "    measurement_duration=5 * ms,\n",
    "    gate_duration=100 * us,\n",
    "    max_parallel_z=3,\n",
    "    max_parallel_xy=3,\n",
    "    max_parallel_c=3,\n",
    "    control_radius=2\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "6342cf6147b2"
   },
   "source": [
    "Note that all above arguments are required to instantiate a `NeutralAtomDevice`. The example device above has the following properties:\n",
    "\n",
    "- The device is defined on a $3 \\times 3$ grid of qubits.\n",
    "- Measurements take $5$ milliseconds.\n",
    "- Gates may take as long as $100$ microseconds if we utilize global microwave gates. Otherwise, a more reasonable bound would be $1$ microsecond.\n",
    "- A maximum of $3$ qubits may be simultaneously acted on by any gate category (`max_parallel_c = 3`).\n",
    "- Controlled gates have next-nearest neighbor connectivity (`control_radius = 2`).\n",
    "\n",
    "We can see some properties of the device as follows."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "id": "de96b2753bec"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Neutral atom device:\n",
      "(0, 0)───(0, 1)───(0, 2)\n",
      "│        │        │\n",
      "│        │        │\n",
      "(1, 0)───(1, 1)───(1, 2)\n",
      "\n",
      "Neighbors of qubit (0, 1):\n",
      "[cirq.GridQubit(1, 1), cirq.GridQubit(0, 2), cirq.GridQubit(0, 0)]\n"
     ]
    }
   ],
   "source": [
    "\"\"\"View some properties of the device.\"\"\"\n",
    "# Display the neutral atom device.\n",
    "print(\"Neutral atom device:\", neutral_atom_device, sep=\"\\n\")\n",
    "\n",
    "# Get the neighbors of a qubit.\n",
    "qubit = cirq.GridQubit(0, 1)\n",
    "print(f\"\\nNeighbors of qubit {qubit}:\")\n",
    "print(neutral_atom_device.neighbors_of(qubit))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "8d75e1232082"
   },
   "source": [
    "## Native gate set"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "v3YMaPw1hfV_"
   },
   "source": [
    "The gates supported by the `NeutralAtomDevice` class can be placed into three categories:\n",
    "\n",
    "1. Single-qubit rotations about the $Z$ axis.\n",
    "2. Single-qubit rotations about an arbitrary axis in the $X$-$Y$ plane. We refer to these as $XY$ gates in this tutorial.\n",
    "3. Controlled gates: CZ, CNOT, CCZ, and CCNOT (TOFFOLI).\n",
    "\n",
    "Any rotation angle is allowed for single-qubit rotations. Some examples of valid single-qubit rotations are shown below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "id": "Hotk4cHCpXCV"
   },
   "outputs": [],
   "source": [
    "\"\"\"Examples of valid single-qubit gates.\"\"\"\n",
    "# Single qubit Z rotations with any angle are valid.\n",
    "neutral_atom_device.validate_gate(cirq.rz(pi / 5))\n",
    "\n",
    "# Single qubit rotations about the X-Y axis with any angle are valid.\n",
    "neutral_atom_device.validate_gate(\n",
    "    cirq.PhasedXPowGate(phase_exponent=pi / 3, exponent=pi / 7)\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "239a75b7d5c6"
   },
   "source": [
    "A Hadamard gate is invalid because it is a rotation in the $X$-$Z$ plane instead of the $X$-$Y$ plane."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "id": "a8b877ad0a87"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "As expected, H is invalid! Unsupported gate: cirq.H\n"
     ]
    }
   ],
   "source": [
    "\"\"\"Example of an invalid single-qubit gate.\"\"\"\n",
    "invalid_gate = cirq.H\n",
    "\n",
    "try:\n",
    "    neutral_atom_device.validate_gate(invalid_gate)\n",
    "except ValueError as e:\n",
    "    print(f\"As expected, {invalid_gate} is invalid!\", e)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "c29c2fd3d197"
   },
   "source": [
    "For controlled gates, the rotation must be a multiple of $\\pi$ due to the physical implementation of the gates. In Cirq, this means the exponent of a controlled gate must be an integer. The next cell shows two examples of valid controlled gates."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "id": "a2c2c1a49432"
   },
   "outputs": [],
   "source": [
    "\"\"\"Examples of valid multi-qubit gates.\"\"\"\n",
    "# Controlled gates with integer exponents are valid.\n",
    "neutral_atom_device.validate_gate(cirq.CNOT)\n",
    "\n",
    "# Controlled NOT gates with two controls are valid.\n",
    "neutral_atom_device.validate_gate(cirq.TOFFOLI)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "62c881b20d63"
   },
   "source": [
    "Any controlled gate with non-integer exponent is invalid."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "id": "ChT4QK7TsabR"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "As expected, CNOT**1.5 is invalid! controlled gates must have integer exponents\n"
     ]
    }
   ],
   "source": [
    "\"\"\"Example of an invalid controlled gate.\"\"\"\n",
    "invalid_gate = cirq.CNOT ** 1.5\n",
    "\n",
    "try:\n",
    "    neutral_atom_device.validate_gate(invalid_gate)\n",
    "except ValueError as e:\n",
    "    print(f\"As expected, {invalid_gate} is invalid!\", e)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "533125e85e19"
   },
   "source": [
    "Multiple controls are allowed as long as every pair of atoms (qubits) acted on by the controlled gate are close enough to each other. We can see this by using the `validate_operation` (or `validate_circuit`) method, as follows."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "id": "59ce97fd61ec"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "As expected, TOFFOLI((0, 0), (1, 0), (0, 2)) is invalid! Qubits cirq.GridQubit(1, 0), cirq.GridQubit(0, 2) are too far away\n"
     ]
    }
   ],
   "source": [
    "\"\"\"Examples of valid and invalid multi-controlled gates.\"\"\"\n",
    "# This TOFFOLI is valid because all qubits involved are close enough to each other.\n",
    "valid_toffoli = cirq.TOFFOLI.on(cirq.GridQubit(0, 0), cirq.GridQubit(0, 1), cirq.GridQubit(0, 2))\n",
    "neutral_atom_device.validate_operation(valid_toffoli)\n",
    "\n",
    "# This TOFFOLI is invalid because all qubits involved are not close enough to each other.\n",
    "invalid_toffoli = cirq.TOFFOLI.on(cirq.GridQubit(0, 0), cirq.GridQubit(1, 0), cirq.GridQubit(0, 2))\n",
    "\n",
    "try:\n",
    "    neutral_atom_device.validate_operation(invalid_toffoli)\n",
    "except ValueError as e:\n",
    "    print(f\"As expected, {invalid_toffoli} is invalid!\", e)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "4ac323a867ad"
   },
   "source": [
    "`NeutralAtomDevice`s do not currently support gates with more than two controls although these are in principle allowed by the physical realizations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "id": "30d77c374b01"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "As expected, CTOFFOLI is invalid! Unsupported gate: cirq.ControlledGate(sub_gate=cirq.TOFFOLI)\n"
     ]
    }
   ],
   "source": [
    "\"\"\"Any gate with more than two controls is invalid.\"\"\"\n",
    "invalid_gate = cirq.ControlledGate(cirq.TOFFOLI)\n",
    "\n",
    "try:\n",
    "    neutral_atom_device.validate_gate(invalid_gate)\n",
    "except ValueError as e:\n",
    "    print(f\"As expected, {invalid_gate} is invalid!\", e)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "88d8a175bcdd"
   },
   "source": [
    "Finally, we note that the duration of any operation can be determined via the `duration_of` method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "id": "a8f7e867f019"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "cirq.Duration(micros=100)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"Example of getting the duration of a valid operation.\"\"\"\n",
    "neutral_atom_device.duration_of(valid_toffoli)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "7f94b0d4ccdf"
   },
   "source": [
    "### Moment and circuit rules"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Ug-2oBPPtZzU"
   },
   "source": [
    "In addition to consisting of valid operations as discussed above, valid moments on a `NeutralAtomDevice` must satisfy the following criteria:\n",
    "\n",
    "1. Only `max_parallel_c` gates of the same category may be performed in the same moment.\n",
    "2. All instances of gates in the same category in the same moment must be identical.\n",
    "3. Controlled gates cannot be applied in parallel with other gate types.\n",
    "  - Physically, this is because controlled gates make use of all types of light used to implement gates.\n",
    "4. Qubits acted on by different controlled gates in parallel must be farther apart than the `control_radius`.\n",
    "  - Physically, this is so that the entanglement mechanism doesn't cause the gates to interfere with one another.\n",
    "5. All measurements must be terminal.\n",
    "\n",
    "Moments can be validated with the `validate_moment` method. Some examples are given below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "id": "Nr-rfUgOtDxE"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example of a valid moment with single-qubit gates:\n",
      "\n",
      "(0, 0): ───Z───\n",
      "\n",
      "(0, 1): ───Z───\n",
      "\n",
      "(0, 2): ───Z───\n",
      "\n",
      "(1, 0): ───X───\n",
      "\n",
      "(1, 1): ───X───\n",
      "\n",
      "(1, 2): ───X───\n"
     ]
    }
   ],
   "source": [
    "\"\"\"Example of a valid moment with single qubit gates.\"\"\"\n",
    "qubits = sorted(neutral_atom_device.qubits)\n",
    "\n",
    "# Get a valid moment.\n",
    "valid_moment = cirq.Moment(cirq.Z.on_each(qubits[:3]) + cirq.X.on_each(qubits[3:6]))\n",
    "\n",
    "# Display it.\n",
    "print(\"Example of a valid moment with single-qubit gates:\", cirq.Circuit(valid_moment), sep=\"\\n\\n\")\n",
    "\n",
    "# Verify it is valid.\n",
    "neutral_atom_device.validate_moment(valid_moment)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "1393b2a6234e"
   },
   "source": [
    "Recall that we defined `max_parallel_z = 3` in our device. Thus, if we tried to do 4 $Z$ gates in the same moment, this would be invalid."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "id": "4b9f3915276b"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example of an invalid moment with single-qubit gates:\n",
      "\n",
      "(0, 0): ───Z───\n",
      "\n",
      "(0, 1): ───Z───\n",
      "\n",
      "(0, 2): ───Z───\n",
      "\n",
      "(1, 0): ───Z───\n"
     ]
    }
   ],
   "source": [
    "\"\"\"Example of an invalid moment with single qubit gates.\"\"\"\n",
    "# Get an invalid moment.\n",
    "invalid_moment = cirq.Moment(cirq.Z.on_each(qubits[:4]))\n",
    "\n",
    "# Display it.\n",
    "print(\"Example of an invalid moment with single-qubit gates:\", cirq.Circuit(invalid_moment), sep=\"\\n\\n\")\n",
    "\n",
    "# Uncommenting raises ValueError: Too many simultaneous Z gates.\n",
    "# neutral_atom_device.validate_moment(invalid_moment)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "5e47c71c4570"
   },
   "source": [
    "This is also true for 4 $XY$ gates since we set `max_parallel_xy = 3`. However, there is an exception for $XY$ gates acting on *every* qubit, as illustrated below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "id": "fa06394e7497"
   },
   "outputs": [],
   "source": [
    "\"\"\"An XY gate can be performed on every qubit in the device simultaneously.\n",
    "\n",
    "If the XY gate does not act on every qubit, it must act on <= max_parallel_xy qubits.\n",
    "\"\"\"\n",
    "valid_moment = cirq.Moment(cirq.X.on_each(qubits))\n",
    "neutral_atom_device.validate_moment(valid_moment)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "89c05574161d"
   },
   "source": [
    "Although both $Z$ and $Z^{1.5}$ are valid gates, they cannot be performed simultaneously because all gates \"of the same type\" must be identical in the same moment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "id": "61112be5f754"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example of an invalid moment with single-qubit gates:\n",
      "\n",
      "(0, 0): ───Z──────\n",
      "\n",
      "(0, 1): ───S^-1───\n"
     ]
    }
   ],
   "source": [
    "\"\"\"Example of an invalid moment with single qubit gates.\"\"\"\n",
    "# Get an invalid moment.\n",
    "invalid_moment = cirq.Moment(cirq.Z(qubits[0]), cirq.Z(qubits[1]) ** 1.5)\n",
    "\n",
    "# Display it.\n",
    "print(\"Example of an invalid moment with single-qubit gates:\", cirq.Circuit(invalid_moment), sep=\"\\n\\n\")\n",
    "\n",
    "# Uncommenting raises ValueError: Non-identical simultaneous Z gates.\n",
    "# neutral_atom_device.validate_moment(invalid_moment)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "55c867d62797"
   },
   "source": [
    "### Appending operations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "l6jE7NnKtjQU"
   },
   "source": [
    "A common pattern for constructing circuits is to append a sequence of operations instead of explicitly creating moments. For a circuit defined on a `NeutralAtomDevice`, Cirq will respect the above rules for creating valid moments.\n",
    "\n",
    "For example, if we append $Z$ and $Z^{1.5}$ from the previous example, Cirq will place them into two moments as shown below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "id": "b93b0f484852"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, 0): ───Z──────────\n",
      "\n",
      "(0, 1): ───────S^-1───\n"
     ]
    }
   ],
   "source": [
    "\"\"\"Cirq satisfies device restrictions automatically when appending operations.\"\"\"\n",
    "# Create a circuit for a NeutralAtomDevice.\n",
    "circuit = cirq.Circuit(device=neutral_atom_device)\n",
    "\n",
    "# Append two gates which cannot be in the same moment.\n",
    "circuit.append([cirq.Z(qubits[0]), cirq.Z(qubits[1]) ** 1.5])\n",
    "\n",
    "# Display the circuit.\n",
    "print(circuit)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "2c4661338f4b"
   },
   "source": [
    "This is true for all device rules. As another example, we can see how Cirq separates controlled gates from other gate types (the third rule above)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "id": "4e5b9a563ec9"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, 0): ───Z───────\n",
      "\n",
      "(0, 1): ───────@───\n",
      "               │\n",
      "(0, 2): ───────X───\n"
     ]
    }
   ],
   "source": [
    "\"\"\"Cirq satisfies device restrictions automatically when appending operations.\"\"\"\n",
    "# Create a circuit for a NeutralAtomDevice.\n",
    "circuit = cirq.Circuit(device=neutral_atom_device)\n",
    "\n",
    "# Append two gates which cannot be in the same moment.\n",
    "circuit.append([cirq.Z(qubits[0]), cirq.CNOT(*qubits[1: 3])])\n",
    "\n",
    "# Display the circuit.\n",
    "print(circuit)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "58bc53b6288a"
   },
   "source": [
    "Without any device restrictions, the `Z` and `CNOT` operations could be in the same moment, but because the circuit is defined on a `NeutralAtomDevice`, the `CNOT` is placed into a new moment."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "35e2090ccb60"
   },
   "source": [
    "### Exercise: Multiple controlled gates in the same moment"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "4b78afc8053d"
   },
   "source": [
    "Construct a `NeutralAtomDevice` which is capable of implementing two `CNOT`s in the same moment. Verify that these operations can indeed be performed in parallel by calling the `validate_moment` method or showing that Cirq inserts the operations into the same moment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "id": "f8b1f29f22c9"
   },
   "outputs": [],
   "source": [
    "# Your code here!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "86a3141cdfb2"
   },
   "source": [
    "#### Solution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "id": "3231b8c17951"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Device:\n",
      "(0, 0)───(0, 1)───(0, 2)\n",
      "│        │        │\n",
      "│        │        │\n",
      "(1, 0)───(1, 1)───(1, 2)\n",
      "\n",
      "Circuit:\n",
      "           ┌──┐\n",
      "(0, 0): ────@─────@───────\n",
      "            │     │\n",
      "(0, 1): ────┼─────┼───@───\n",
      "            │     │   │\n",
      "(0, 2): ────┼@────┼───┼───\n",
      "            ││    │   │\n",
      "(1, 0): ────X┼────X───┼───\n",
      "             │        │\n",
      "(1, 1): ─────┼────────X───\n",
      "             │\n",
      "(1, 2): ─────X────────────\n",
      "           └──┘\n"
     ]
    }
   ],
   "source": [
    "\"\"\"Example solution for creating a device which allows two CNOTs in the same moment.\"\"\"\n",
    "# Create a NeutralAtomDevice.\n",
    "device = cirq.NeutralAtomDevice(\n",
    "    qubits=cirq.GridQubit.rect(2, 3),\n",
    "    measurement_duration=5 * cirq.Duration(nanos=10**6),\n",
    "    gate_duration=100 * cirq.Duration(nanos=10**3),\n",
    "    max_parallel_z=4,\n",
    "    max_parallel_xy=4,\n",
    "    max_parallel_c=4,\n",
    "    control_radius=1\n",
    ")\n",
    "print(\"Device:\")\n",
    "print(device)\n",
    "\n",
    "# Create a circuit for a NeutralAtomDevice.\n",
    "circuit = cirq.Circuit(device=device)\n",
    "\n",
    "# Append two CNOTs that can be in the same moment.\n",
    "circuit.append(\n",
    "    [cirq.CNOT(cirq.GridQubit(0, 0), cirq.GridQubit(1, 0)), \n",
    "     cirq.CNOT(cirq.GridQubit(0, 2), cirq.GridQubit(1, 2))]\n",
    ")\n",
    "\n",
    "# Append two CNOTs that cannot be in the same moment.\n",
    "circuit.append(\n",
    "    [cirq.CNOT(cirq.GridQubit(0, 0), cirq.GridQubit(1, 0)), \n",
    "     cirq.CNOT(cirq.GridQubit(0, 1), cirq.GridQubit(1, 1))]\n",
    ")\n",
    "\n",
    "# Display the circuit.\n",
    "print(\"\\nCircuit:\")\n",
    "print(circuit)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "d29db15ac256"
   },
   "source": [
    "Note that the square brackets above/below the circuit indicate the first two `CNOT`s are in the same moment."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "49fc5658b4a5"
   },
   "source": [
    "## Decomposing operations and circuits"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "2f513654524d"
   },
   "source": [
    "Invalid operations can be decomposed into valid operations via the `decompose_operation` method. For example, we saw above that `cirq.H` was an invalid gate for a `NeutralAtomDevice`. This can be decomposed into valid operations as follows."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "id": "6fe3d596c504"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Circuit for H on a NeutralAtomDevice:\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"overflow: auto; white-space: pre;\">(0, 0): ───PhX(-0.5)^0.5───Z───</pre>"
      ],
      "text/plain": [
       "(0, 0): ───PhX(-0.5)^0.5───Z───"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"Example of decomposing an operation.\"\"\"\n",
    "# Decompose a Hadamard operation.\n",
    "ops = neutral_atom_device.decompose_operation(cirq.H.on(qubits[0]))\n",
    "\n",
    "# Display the circuit.\n",
    "print(\"Circuit for H on a NeutralAtomDevice:\\n\")\n",
    "cirq.Circuit(ops, device=neutral_atom_device)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "eee4f54bbc65"
   },
   "source": [
    "Two-qubit and other operations can be decomposed in an analogous manner, for example the `FSimGate` below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "id": "9fb75abc3504"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Circuit for FSim on a NeutralAtomDevice:\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"overflow: auto; white-space: pre;\">(0, 0): ───PhX(-0.698)^0.5───@───PhX(-0.698)^0.5──────────────────────@───PhX(0.85)^0.5──────────────────────@───PhX(0.85)^0.5───Z^-0.095───\n",
       "                             │                                        │                                      │\n",
       "(1, 0): ─────────────────────@─────────────────────PhX(0.802)^0.968───@───────────────────PhX(0.802)^0.968───@───Z^0.452────────────────────</pre>"
      ],
      "text/plain": [
       "(0, 0): ───PhX(-0.698)^0.5───@───PhX(-0.698)^0.5──────────────────────@───PhX(0.85)^0.5──────────────────────@───PhX(0.85)^0.5───Z^-0.095───\n",
       "                             │                                        │                                      │\n",
       "(1, 0): ─────────────────────@─────────────────────PhX(0.802)^0.968───@───────────────────PhX(0.802)^0.968───@───Z^0.452────────────────────"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"Another example of decomposing an operation.\"\"\"\n",
    "# Decompose an FSimGate operation.\n",
    "ops = neutral_atom_device.decompose_operation(\n",
    "    cirq.FSimGate(theta=0.1, phi=0.3).on(cirq.GridQubit(0, 0), cirq.GridQubit(1, 0))\n",
    ")\n",
    "\n",
    "# Display the circuit.\n",
    "print(\"Circuit for FSim on a NeutralAtomDevice:\\n\")\n",
    "cirq.Circuit(ops, device=neutral_atom_device)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "391928d22b34"
   },
   "source": [
    "> *Note*: As with any proposed architecture for quantum computing, several research groups around the world are working towards a device based on neutral atom qubits. Each research group has a different approach, such as using different atomic species or working with a different number of dimensions of atomic qubit arrays. As such, the `NeutralAtomDevice` class will not accurately reflect all such devices. The class is based on the two dimensional Cesium array at the University of Wisconsin-Madison in the research group of Mark Saffman. Development of this device is being pursued as part of a strategic partnership between the University of Wisconsin-Madison and ColdQuanta."
   ]
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [],
   "name": "neutral_atom.ipynb",
   "toc_visible": true
  },
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
